package com.haoze.nurse.controller;

import com.haoze.common.response.Result;
import com.haoze.common.response.ResultGenerator;
import com.haoze.nurse.dto.OrderPageDto;
import com.haoze.nurse.dto.OrdersSignDTO;
import com.haoze.nurse.dto.lab.LabSpecimenSearchDto;
import com.haoze.nurse.enumeration.OrderQueryStatusTypeEnum;
import com.haoze.nurse.service.FileIndexService;
import com.haoze.nurse.service.OrderService;
import com.haoze.nurse.service.PatientOrderService;
import com.haoze.nurse.vo.Commodel;
import com.haoze.nurse.vo.DoctorAdvicesVO;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/nurse/order")
@Api(value = "医嘱信息目录")
public class OrderController {

    @Resource
    OrderService orderService;


    @GetMapping("/orderInfoApp")
    @ApiOperation(value = "医嘱信息列表App")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "times", value = "时间", example = "2020-05-05", required = true),
            @ApiImplicitParam(name = "inpatNo", value = "住院号", dataType = "String", paramType = "query", example = "12345678_1", required = true),
            @ApiImplicitParam(name = "type", value = "医嘱类型", dataType = "String", paramType = "query", example = "long长期医嘱，tmp临时医嘱", required = false),
            @ApiImplicitParam(name = "status", value = "医嘱状态", dataType = "String", paramType = "query", example = "new新开医嘱，unload未执行，loading执行中，loaded已执行", required = false),
            @ApiImplicitParam(name = "orderStatusTypeValue", value = "医嘱状态(1-全部 2-新开 Arrange3-整理 Stoped 4-已停)")
    })
    public Result orderInfoApp(
            @RequestParam String inpatNo,
            @RequestParam String times,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) Integer orderStatusTypeValue,
            @RequestParam Integer page,
            @RequestParam Integer pageSize) {
        if (StringUtils.isEmpty(times)) {
            throw new RuntimeException("请选择时间");
        }
        OrderPageDto orderPageDto = new OrderPageDto();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(times)) {
            times += " 23:59:59";
            orderPageDto.setEndDate(times);
        }
        orderPageDto.setPageNum(page);
        orderPageDto.setPageSize(pageSize);
        orderPageDto.setStatus(status);
        orderPageDto.setType(type);
        orderPageDto.setInpatNo(inpatNo);
        if (orderStatusTypeValue != null && orderStatusTypeValue.intValue() > 0) {
            orderPageDto.setOrderQueryStatusValue(orderStatusTypeValue);
            orderPageDto.setOrderQueryStatusType(OrderQueryStatusTypeEnum.getById(orderStatusTypeValue));
        }

        return ResultGenerator.genOkResult(orderService.queryPagesList(orderPageDto));
    }

    @GetMapping("/orderDetail")
    @ApiOperation(value = "医嘱明细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatorderdataId", value = "医嘱编号", dataType = "String", paramType = "query", example = "12345678_1", required = true),
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query", example = "2020-05-05", required = true),
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query", example = "2020-05-07", required = true),
    })
    public Result orderDetail(@RequestParam String inpatorderdataId, @RequestParam String startDate, @RequestParam String endDate, @RequestParam Integer page, @RequestParam Integer pageSize) {
        return orderService.orderDetail(inpatorderdataId, startDate, endDate, page, pageSize);
    }


    @GetMapping("/itineraryListNew")
    @ApiOperation(value = "巡回单查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNumList", value = "选择的病人住院号,多个逗号拼接", dataType = "String", paramType = "query", example = "12345678_1", required = true),
            @ApiImplicitParam(name = "date", value = "查询时间", dataType = "String", paramType = "query", example = "2020-05-07", required = true),
            @ApiImplicitParam(name = "orderType", value = "查询类型 全部 all 治疗单 treatment 服药单 medication 注射单 injection 其他给药单 orther 雾化单 atomization 肠内营养 nutrition 输液单 infusion", dataType = "String", paramType = "query", example = "2020-05-05", required = true),
            @ApiImplicitParam(name = "printFlag", value = "打印标识   printed已打印  not_printed未打印", dataType = "String", paramType = "query", example = "2020-05-07", required = true),
            @ApiImplicitParam(name = "page", value = "分页页数", dataType = "String", paramType = "query", example = "2020-05-05", required = true),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", dataType = "String", paramType = "query", example = "2020-05-07", required = true)
    })
    public Result itineraryListNew(@RequestParam List<String> inpatNumList, @RequestParam String date, @RequestParam String orderType, @RequestParam String printFlag) {
        if (inpatNumList == null || inpatNumList.size() == 0) {
            return ResultGenerator.genFailedResult("请选择病人");
        }
        return orderService.itineraryListNew(inpatNumList, orderType, printFlag, date);
    }

    @GetMapping("/getExecuteDetail")
    @ApiOperation(value = "获取执行详情")
    public Result getExecuteDetail(@RequestParam String qrCode,@RequestParam String freqData) {
        return orderService.getExecuteDetail(qrCode,freqData);
    }

    @PostMapping("/print")
    @ApiOperation(value = "口服药打印列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "模型名称", dataType = "String", paramType = "modelName")
    })
    public Result print(@RequestBody List<Map<String, Object>> data) {
        return orderService.prints(data);
    }


    @GetMapping("/treatmentList")
    @ApiOperation(value = "治疗单查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inpatNumList", value = "选择的病人住院号,多个逗号拼接", dataType = "String", paramType = "query", example = "12345678_1", required = true),
            @ApiImplicitParam(name = "date", value = "查询时间", dataType = "String", paramType = "query", example = "2020-05-07", required = true),
            @ApiImplicitParam(name = "recordDate", value = "记录查询时间", dataType = "String", paramType = "query", example = "2020-05-07", required = true),
            @ApiImplicitParam(name = "orderType", value = "查询类型 全部 all 治疗单 treatment 服药单 medication 注射单 injection 其他给药单 orther 雾化单 atomization 肠内营养 nutrition 输液单 infusion", dataType = "String", paramType = "query", example = "2020-05-05", required = true)
    })
    public Result treatmentList(@RequestParam(required = false) List<String> inpatNumList, @RequestParam String date
            , @RequestParam String orderType, @RequestParam(required = false) String recordDate, @RequestParam(required = false) String wardDeptId) {
        if (StringUtils.isNotBlank(recordDate)) {
            recordDate = recordDate.replaceAll("u002B", " ");
        }
        return orderService.treatmentList(inpatNumList, wardDeptId, orderType, date, recordDate);
    }

    @GetMapping("/treatmentDateList")
    @ApiOperation(value = "治疗单时间列表")
    public Result treatmentDateList(@RequestParam(required = false) List<String> inpatNumList, @RequestParam String date
            , @RequestParam String orderType, @RequestParam(required = false) String wardDeptId) {
        return orderService.treatmentDateList(inpatNumList, orderType, date, wardDeptId);
    }

    @PostMapping("/treatmentPrint")
    @ApiOperation(value = "治疗单打印")
    public Result treatmentPrint(@RequestBody List<Map<String, Object>> data) {
        return orderService.printTreatment(data);
    }

    @GetMapping("/print")
    @ApiOperation(value = "医嘱打印")
    public Result print(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer pageSize, @RequestParam String type, @RequestParam String inpatNum
            , @RequestParam(required = false) String deptId, @RequestParam(required = false) String operationFlag, @RequestParam(required = false) String startTime
            , @RequestParam(required = false) String endTime, @RequestParam(required = false) String inpatorderdataId) {
        //TYPE 1长期医嘱  type2 临时医嘱
        if (StringUtils.isBlank(operationFlag)) {//0-非急诊 1-急诊
            operationFlag = "0";
        }
        /*if ("3".equals(type)) {
            type = null;
        }*/
        return ResultGenerator.genOkResult(orderService.printOrder(type, inpatNum, page, pageSize, deptId, operationFlag, startTime, endTime,inpatorderdataId));
    }
    @GetMapping("/printQuery")
    @ApiOperation(value = "医嘱打印")
    public Result printQuery(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer pageSize, @RequestParam String type, @RequestParam String inpatNum
            , @RequestParam(required = false) String deptId, @RequestParam(required = false) String operationFlag, @RequestParam(required = false) String startTime
            , @RequestParam(required = false) String endTime, @RequestParam(required = false) String inpatorderdataId) {
        //TYPE 1长期医嘱  type2 临时医嘱
        if (StringUtils.isBlank(operationFlag)) {//0-非急诊 1-急诊
            operationFlag = "0";
        }
       /* if ("3".equals(type)) {
            type = null;
        }*/
        return ResultGenerator.genOkResult(orderService.printOrder1(type, inpatNum, page, pageSize, deptId, operationFlag, startTime, endTime,inpatorderdataId));
    }

    @GetMapping("/printNum")
    @ApiOperation(value = "医嘱打印")
    public Result printNum(@RequestParam String type, @RequestParam String inpatNum, @RequestParam(required = false) String startTime
            , @RequestParam(required = false) String endTime, @RequestParam(required = false) String deptId, @RequestParam(required = false) String operationFlag) {
        if (StringUtils.isBlank(operationFlag)) {
            operationFlag = "0";
        }
        //TYPE 1长期医嘱  type2 临时医嘱  type3 术前医嘱
        return orderService.printNum(type, inpatNum, deptId, operationFlag, startTime, endTime);
    }

    @GetMapping("/printGetInfo")
    @ApiOperation(value = "医嘱打印病人信息获取")
    public Result printGetInfo(@RequestParam String inpatNum) {
        //TYPE 1长期医嘱  type2 临时医嘱
        return orderService.printInfo(inpatNum);
    }

    @PostMapping("/sign")
    @ApiOperation(value = "医嘱签名")
    public Result sign(@RequestBody List<OrdersSignDTO> dto) {
        return orderService.ordersSign(dto);
    }

    @GetMapping("/fileindexSign")
    @ApiOperation(value = "签名")
    public Result fileindexSign(@RequestParam final String loginName,
                                @RequestParam final String userPwd,
                                @RequestParam(required = false) final String emrType,
                                @RequestParam(required = false) final String comparedUserId,
                                @RequestParam(required = false) final String varCode) {
        return orderService.sign(loginName, userPwd, comparedUserId, emrType, varCode);
    }
}
